//add.js
//获取应用实例
var app = getApp()

Page({
    data: {
        id: 1,
        name: "胥智",
        phone: "18280086457",
        province: "四川省",
        city: "成都市",
        country: "青羊区",
        detail: "成飞大道199号",
        sex: "1",
        email: "5thgfka@gmail.com",
        "items": [
            { name: '1', value: '男' },
            { name: '0', value: '女' },
        ]
    },

    onLoad: function(options) {
        let clientId = options.clientid;
        let name = options.name;
        let province = options.province;
        let city = options.city;
        let country = options.country;
        let detail = options.detail;
        let phone = options.phone;
        let sex = options.sex;
        let email = options.email;
        let remark = options.remark;

        let items = this.data.items;
        items.map(function(it) {
            if (it.name == sex) {
                it.checked = true;
            }
        });

        this.setData({
            id: clientId,
            name: name,
            province: province,
            city: city,
            country: country,
            detail: detail,
            sex: sex,
            email: email,
            phone: phone,
            items: items,
            remark: remark
        })
    },
    // 输入框事件函数
    nameInput: function(event) {
        let ipts = event.detail.value;
        this.setData({
            name: ipts
        })
    },
    phoneInput: function(event) {
        let ipts = event.detail.value;
        this.setData({
            phone: ipts
        })
    },
    emailInput: function(event) {
        let ipts = event.detail.value;
        this.setData({
            email: ipts
        })
    },
    remarkInput: function(event) {
        let ipts = event.detail.value;
        this.setData({
            remark: ipts
        })
    },
    radioChange: function(event) {
        let ipts = event.detail.value;
        this.setData({
            sex: ipts
        });
    },
    addressTap: function() {
        let that = this;
        wx.chooseAddress({
            success(res) {
                that.setData({
                    province: res.provinceName,
                    city: res.cityName,
                    country: res.countyName,
                    detail: res.detailInfo,
                    phone: res.telNumber,
                    name: res.userName
                })
                // console.log(res.userName)
                // console.log(res.postalCode)
                // console.log(res.provinceName)
                // console.log(res.cityName)
                // console.log(res.countyName)
                // console.log(res.detailInfo)
                // console.log(res.nationalCode)
                // console.log(res.telNumber)
            }
        })
    },
    // 提交更新数据
    submitTap: function() {
        // 保存用户申请
        let thirdSession = wx.getStorageSync('thirdSession');
        let id = this.data.id;
        let name = this.data.name;
        let email = this.data.email;
        let phone = this.data.phone;
        let remark = this.data.remark;
        let province = this.data.province;
        let city = this.data.city;
        let country = this.data.country;
        let detail = this.data.detail;
        let sex = this.data.sex;
        // 判断用户输入信息是否规范
        // @todo
        wx.request({
            url: 'https://xsounder.com/address/updateCustomer/',
            method: 'POST',
            data: {
                thirdSession: thirdSession,
                id: id,
                name: name,
                email: email,
                phone: phone,
                province: province,
                city: city,
                country: country,
                detail: detail,
                sex: sex,
                remark: remark
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status == 'fail') {
                    wx.showToast({
                        title: '更新失败!',
                        icon: 'none',
                        duration: 2000,
                        mask: true,
                    })
                } else {
                    wx.showToast({
                        title: '更新成功!',
                        icon: 'none',
                        duration: 2000,
                        mask: true,
                        success: function() {
                            setTimeout(function() { wx.navigateBack(); }, 2000)
                        }
                    })
                }

            }
        });
    }
})